//index.js
import utils from "../../utils/util.js";
//获取应用实例
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    hotelName: 'XX智慧酒店',
    address: '余杭区良睦路1288号梦想小镇2号楼',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
 
  // 加载时展示日期
  onLoad: function (options) {
      var that = this;
      var _out = new Date();
      _out.setDate(_out.getDate() + 1);
      var now = new Date();
      that.setData({
        checkInDate: utils.formatTime2(new Date()),
        checkOutDate: utils.formatTime2(_out),
        checkInVal: utils.formatTime(new Date()),
        checkOutVal: utils.formatTime(_out),
        days: utils.getDays(now, _out),
      });
      // 清除日历缓存
      wx.removeStorage({
        key: 'ROOM_SOURCE_DATE',
        success: function (res) {
          console.log(res.data)  // undefined
        }
      })
      // 加载请求
      // wx.request({
      //   url: 'http://testcloud.resthour.net/wx/hotel/getHotel?hotelId=2',
      //   // data: data,
      //   success: function (res) {
      //     // console.log(res);
      //     that.setData({
      //       hotelName: res.data.data.name
      //     })
      //   }
      // });
  },

  // 手动选择日期之后展示
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'ROOM_SOURCE_DATE',
      success: function(res) {
        that.setData({
          checkInDate: res.data.checkInDate.slice(5),
          checkOutDate: res.data.checkOutDate.slice(5),
          checkInVal: res.data.checkInDate + ' 06:00:00',
          checkOutVal: res.data.checkOutDate + " 12:00:00",
          days: res.data.days
        })
      },
    });
  },
  standRoom: function (e) {
     var checkInVal = this.data.checkInVal,
       checkOutVal = this.data.checkOutVal;
    wx.navigateTo({
       url: '../roomType/roomType?hotelId=' + 2 + '&adate=' + checkInVal + '&ddate=' + checkOutVal,
    })

  },
  // 打开地图
  openLocation(){
    var that = this;
      wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success: function (res) {
          wx.openLocation({
            latitude: 30.285920,
            longitude: 120.005830,
            scale: 16,
            name: that.data.hotelName,
            address: that.data.address
          })
        },
        fail: function (res) { //用户点了“拒绝”
          wx.showModal({ // 向用户提示需要权限才能继续
            // title: '提示',
            content: '地图功能需要您提供地理位置授权！',
            success: function (res) {
              if (res.confirm) {
                wx.openSetting({
                  success: function (res) { // 如果成功打开授权
                    if (res.authSetting["scope.userLocation"]) {
                      wx.openLocation({
                        latitude: 30.285920,
                        longitude: 120.005830,
                        scale: 16,
                        name: that.data.hotelName,
                        address: that.data.address
                      })
                    }
                    else {// 如果用户依然拒绝授权

                    }
                  },
                  fail: function () { //调用失败，授权登录不成功
                    fail()
                  }
                })
              } else if (res.cancel) {
                return false
              }
            }
          }) 
         
     }
   })
 },
 // 分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '杭州睿沃智慧酒店',
      path: 'page/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})
